#include "UDPSendThread.h"

UDPSendThread::UDPSendThread(UDPProcessThread &process):udpProcess(process)
{
	stop = false;
}

UDPSendThread::~UDPSendThread()
{
	Stop();
}
#if 0 
int UDPSendThread::UDPClient(const char *ip, unsigned short port)
{

	udpProcess.UDPClient(ip,port);
	return 0;
}
#endif

int UDPSendThread::Start()
{
	if (JThread::IsRunning())
	{
		return -5;
	}
	if (!stopmutex.IsInitialized())
	{
		if (stopmutex.Init()<0)
		{
			return -1;
		}
	}
	stop = false;
	if (JThread::Start()<0)
	{
		return -2;
	}
	return 0;
}

void UDPSendThread::Stop()
{
	if (!JThread::IsRunning())
	{
		return ;
	}
	stopmutex.Lock();
	stop = true;
	stopmutex.Unlock();
	if (JThread::IsRunning())
	{
		JThread::Kill();
	}
	stop = false;
}

void* UDPSendThread::Thread()
{
	beginTime = time(NULL) - RTP_HEATBEAT_SENDTIMEOUT-1;
	JThread::ThreadStarted();
	while (stop == false)
	{
		if (time(NULL) - beginTime > RTP_HEATBEAT_SENDTIMEOUT){
			beginTime = time(NULL);
			int ret = udpProcess.SendRtpHeatbeatData();
			if (ret != 0){
				udpProcess.Close();
				udpProcess.UDPServer();
				Sleep(500);
			}
		}
		udpProcess.SendData();
	}
	return NULL;
}